﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ppt = Microsoft.Office.Interop.PowerPoint;

namespace ObjectModel.PowerPoint
{
    public class PresentationEventsManager
    {
        readonly Ppt.Application mApp = Globals.Application;
        readonly Dictionary<Ppt.Presentation, PresentationEvents> mDict = new Dictionary<Ppt.Presentation, PresentationEvents>();

        public PresentationEventsManager()
        {
            mApp.PresentationClose += App_PresentationClose;
            mApp.PresentationNewSlide += App_PresentationNewSlide;
            mApp.PresentationBeforeSave += App_PresentationBeforeSave;
            mApp.SlideSelectionChanged += App_SlideSelectionChanged;
            mApp.WindowSelectionChange += App_WindowSelectionChange;
        }

        public PresentationEvents this[Ppt.Presentation key]
        {
            get
            {
                if (!mDict.ContainsKey(key))
                    mDict.Add(key, new PresentationEvents());

                return mDict[key];
            }
        }

        void App_PresentationClose(Ppt.Presentation Pres)
        {
            if (mDict.ContainsKey(Pres))
                mDict.Remove(Pres);
        }

        void App_PresentationNewSlide(Ppt.Slide Sld)
        {
            if (mDict.ContainsKey(mApp.ActivePresentation))
                mDict[mApp.ActivePresentation].OnPresentationNewSlide(Sld);
        }

        void App_PresentationBeforeSave(Ppt.Presentation Pres, ref bool Cancel)
        {
            if (mDict.ContainsKey(Pres))
                mDict[Pres].OnPresentationBeforeSave(Pres);
        }


        void App_SlideSelectionChanged(Ppt.SlideRange SldRange)
        {
            if (mDict.ContainsKey(mApp.ActivePresentation))
                mDict[mApp.ActivePresentation].OnSlideSelectionChanged(SldRange);
        }

        void App_WindowSelectionChange(Ppt.Selection Sel)
        {
            if (mDict.ContainsKey(mApp.ActivePresentation))
                mDict[mApp.ActivePresentation].OnWindowSelectionChange(Sel);
        }
    }
}
